package com.army.study;

import com.army.study.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.bundle.IBundleManager;
import ohos.security.SystemPermission;

import java.util.ArrayList;
import java.util.List;

public class MainAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());

        // 动态授权
        requestPermissions();
    }

    private void requestPermissions() {
        String[] permissions = {
                // 分布式数据同步权限
                SystemPermission.DISTRIBUTED_DATASYNC,
                // 读取用户存储权限
                SystemPermission.READ_USER_STORAGE,
                // 写用户存储权限
                SystemPermission.WRITE_USER_STORAGE
        };
        List<String> permissionsToProcess = new ArrayList<>();
        for (String permission : permissions) {
            if (verifySelfPermission(permission) != IBundleManager.PERMISSION_GRANTED
                    && canRequestPermission(permission)) {
                permissionsToProcess.add(permission);
            }
        }
        requestPermissionsFromUser(permissionsToProcess.toArray(new String[0]), 0);
    }
}
